#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"

/*
typedef struct peodatas
{
    char name[32];
    int age;
    int IDnum;
}peo_data;

typedef struct peonode
{
    peo_data datas;
    struct peonode *next;
}link_node;
*/

link_node *creat_head(void)
{
    link_node *H=(link_node *)malloc(sizeof(link_node));
    strcpy(H->datas.name,"hello");
	H->datas.age=0;
	H->datas.IDnum=0;
    
    H->next=NULL;

    return H;
} 

int head_insert(link_node *H)
{
	link_node *q=H;
	char inputname[32];
	int inputage;
	int inputIDnum;
	printf("******plz input name >>\n");
	scanf("%s",inputname);
	printf("******plz input age  >>\n");
	scanf("%d",&inputage);
	printf("******plz input IDnum>>\n");
	scanf("%d",&inputIDnum);
	
	printf("%s %d %d",inputname,inputage,inputIDnum);
	link_node *p=(link_node *)malloc(sizeof(link_node));

	strcpy(p->datas.name,inputname);
	p->datas.age=inputage;
	p->datas.IDnum=inputIDnum;
	printf("%s %d %d",p->datas.name,p->datas.age,p->datas.IDnum);

	p->next=q->next;
	q->next=p;

	return 0;
}

int tail_insert(link_node *H)
{
	link_node *q=H;
	char inputname[32];
	int inputage;
	int inputIDnum;
	printf("******plz input name >>\n");
	scanf("%s",inputname);
	printf("******plz input age  >>\n");
	scanf("%d",&inputage);
	printf("******plz input IDnum>>\n");
	scanf("%d",&inputIDnum);
	
	link_node *p=(link_node *)malloc(sizeof(link_node));

	strcpy(p->datas.name,inputname);
	p->datas.age=inputage;
	p->datas.IDnum=inputIDnum;

	while(q->next!=NULL)
	{
		q=q->next;
	}

	p->next=q->next;
	q->next=p;

	return 0;
}

int pos_insert(link_node *H)
{
	link_node *q=H;
	char inputname[32];
	int inputage;
	int inputIDnum;
	int inputpos;
	printf("******plz input pos  >>\n");
	scanf("%d",&inputpos);
	printf("******plz input name >>\n");
	scanf("%s",inputname);
	printf("******plz input age  >>\n");
	scanf("%d",&inputage);
	printf("******plz input IDnum>>\n");
	scanf("%d",&inputIDnum);
	
	link_node *p=(link_node *)malloc(sizeof(link_node));

	strcpy(p->datas.name,inputname);
	p->datas.age=inputage;
	p->datas.IDnum=inputIDnum;

	for(int i=0;i<inputpos;i++)
	{
		q=q->next;
	}
	
	p->next=q->next;
	q->next=p;

	return 0;
}

int head_delete(link_node *H)
{
	link_node *p=H->next;
	H->next=p->next;
	free(p);

	return 0;
}

int tail_delete(link_node *H)
{
	link_node *p=H;
	while(p->next->next!=NULL)
	{
		p=p->next;
	}
	link_node *q=p->next;

	p->next=q->next;
	free(q);

	return 0;
}

int pos_delete(link_node *H)
{
	link_node *p=H;
	int inputIDnum;
	int inputpos;
	printf("******plz input pos  >>\n");
	scanf("%d",&inputpos);

	for(int i=0;i<inputpos-1;i++)
	{
		p=p->next;
	}
	link_node *q=p->next;

	p->next=q->next;
	free(q);

	return 0;
}

int view(link_node *H)
{
	link_node *p=H;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("-----------------\n");
		printf("----name=%s\n",p->datas.name);
		printf("----age=%d\n",p->datas.age);
		printf("----IDnum=%d\n",p->datas.IDnum);
		printf("-----------------\n");
	}

	return 0;
}
